﻿using System.Collections.Generic;
using System.Text;
using ParticlesEngine.Core;
using SlimDX;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using SlimDX.D3DCompiler;

namespace ParticlesEngine.Primitives
{
    public class Triangle : GraphicComponent
    {
        DataStream vertices;
        InputElement[] elements;
        InputLayout layout;
        Buffer vertexBuffer;
        

        public Triangle()
        {
        }

        public override void Draw()
        {
            
            context.InputAssembler.InputLayout = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 32, 0));
            
            for (int i = 0; i < technique.Description.PassCount; ++i)
            {
                pass.Apply(context);
                context.Draw(3, 0);
            }

        }
        public override void Initialize()
        {
            vertices = new DataStream(32 * 3, true, true);
            vertices.WriteRange(new[] {
                new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)
            });
            vertices.Position = 0;

            elements = new InputElement[] { 
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)  
            };
            vertexBuffer = new Buffer(device, vertices, 32 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None,
                0);
            vertices.Dispose();

            technique = effect.GetTechniqueByIndex(0);
            pass = technique.GetPassByIndex(0);
            layout = new InputLayout(device, elements, pass.Description.Signature);
            
        }

        public override void Update()
        {
            
        }
        public override void Dispose()
        {
            
            vertices.Dispose();
            vertexBuffer.Dispose();
        }
    }
}
